﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using WindowsForms_Logic_Questions_Generator.Operators;

namespace WindowsForms_Logic_Questions_Generator
{
    class DocumentParser
    {
        private Deserialization m_Deserialization;
        private Serialization m_Serialization;
        private DocumentExtraData m_Doc;
        private ExamGenXML m_XMLDoc;
        private RandomQuestionsGenerator m_Gen;
        
        public DocumentParser(DocumentExtraData Doc)
        {
            m_Deserialization = new Deserialization();
            m_Serialization = new Serialization();
            m_XMLDoc  = new ExamGenXML();
            m_Gen = new RandomQuestionsGenerator();
            m_Doc = Doc;
        }

        public bool SaveTest(ListQestions questionsList, bool bGenerate)
        {
            String appPath = "";

            appPath = m_Serialization.SaveTest(m_XMLDoc.ConvertDataToXML(m_Doc, questionsList));

            if (bGenerate && (0 != appPath.Length))
            {
                //appPath = "C:\\Users\\Public\\tempTest.rtf";
                return m_Gen.GenerateRandomsQuestions(m_Doc, questionsList, appPath);
            }
            return false;
        }

        public bool LoadTest(ListQestions questionsList)
        {
            m_Doc.SetExtraData();
            XmlDocument xmlDoc = m_Deserialization.LoadTest();
            if (null == xmlDoc)
            {
                return false;
            }            
            if (m_XMLDoc.ConvertXMLToData(xmlDoc, m_Doc, questionsList))
            {
                return true;
            }
            else
            {
                const string message = "Problem with template test file, Canceling loading reques.";
                const string caption = "Attention";
                MessageBox.Show(message, caption,
                                             MessageBoxButtons.OK,
                                             MessageBoxIcon.Error);
            }
            return false;
        }
    }

    

    
}
